package com.wlzg.collectiontool.view

import android.content.Context
import android.util.Log
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.lxj.xpopup.core.CenterPopupView
import com.wlzg.collectiontool.R
import com.wlzg.collectiontool.adapter.TongbuAdapter
import com.wlzg.collectiontool.db.AppDatabase
import com.wlzg.collectiontool.model.bean.*
import com.wlzg.collectiontool.util.ExcelUtils
import com.wlzg.collectiontool.util.FileUtil
import kotlinx.android.synthetic.main.dia_tongbu.view.*
import kotlinx.android.synthetic.main.dia_tongbu.view.diss
import kotlinx.android.synthetic.main.dia_tongbu.view.recycler
import kotlinx.android.synthetic.main.dia_tongbu.view.status
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class TongbuDia(var mContext:Context, var mFragment:InvestMapFragment) : CenterPopupView(mContext),
    View.OnClickListener {
    private var dataList: ArrayList<PsGongcheng?>? = ArrayList()
    private var mAdapter: TongbuAdapter = TongbuAdapter()
    private var tbcode:Int =0
    private var gccode:Int=0
    override fun getImplLayoutId(): Int {
        return R.layout.dia_tongbu
    }

    override fun onCreate() {
        super.onCreate()
        diss.setOnClickListener(this)
        chaxun.setOnClickListener(this)
        daochu.setOnClickListener(this)
        status.setItems("进行中","完成")
        status.setOnItemSelectedListener { view, position, id, item ->
            status.setText(item as? String)
            gccode=position
        }
        tbstatus.setItems("未同步","同步成功","同步失败")
        tbstatus.setOnItemSelectedListener { view, position, id, item ->
            tbstatus.setText(item as? String)
            tbcode=position
        }
        mcheck.setOnCheckedChangeListener { compoundButton, b ->

                if(dataList!=null){
                for(it in dataList!!){
                    if(b){
                    it?.ifchecked=1
                    }else{
                        it?.ifchecked=0
                    }
                }
                    mAdapter.notifyDataSetChanged()

                }

        }
        initData()

    }


    private suspend fun filter(list: ArrayList<PsGongcheng?>?){
        if (list != null) {
            for (m in list){
                val xm= m?.ssxmId?.let {
                    AppDatabase.getInstance().userDao().getXmListbyId(
                        it
                    )
                }
                m?.ssxmName=xm?.xmmc
                m?.totalWork= xm?.totalWork!!
            }
        }
        dataList?.clear()
        list?.let {
            dataList?.addAll(it)

        }
    }
    private fun initData() {
        recycler.adapter=mAdapter
        recycler.layoutManager= LinearLayoutManager(context, LinearLayoutManager.VERTICAL,false)
        mAdapter.setNewData(dataList)
        mFragment.lifecycleScope.launch(context = Dispatchers.Main) {
            withContext(Dispatchers.IO) {
                val list = AppDatabase.getInstance().userDao().getGcList() as ArrayList<PsGongcheng?>?
               filter(list)
            }
            mAdapter.notifyDataSetChanged()
        }



        mAdapter.setOnItemClickListener {
                adapter, view, position ->
            dataList?.get(position)?.ifchecked=if(dataList?.get(position)?.ifchecked==0)1 else 0
            mAdapter.notifyDataSetChanged()

        }
    }

    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.diss->{
                dismiss()
            }
            R.id.chaxun->{
                query()
            }
            R.id.daochu->{
                export()

            }
        }
    }
    fun query(){
        var gcmc:String?=gcmc.text.toString()
        var xmmc:String?=ssxmmc.text.toString()
       if(gcmc!=null&&gcmc.isEmpty()){
           gcmc=null
       }
        if(xmmc!=null&&xmmc.isEmpty()){
            xmmc=null
        }

        mFragment.lifecycleScope.launch(context = Dispatchers.Main) {
            withContext(Dispatchers.IO) {
          val list=  AppDatabase.getInstance().userDao().gettbgcList(xmmc,gcmc,gccode.toString(),tbcode)as ArrayList<PsGongcheng?>?
                filter(list)
            }
            mAdapter.notifyDataSetChanged()
        }

    }
    var gclist:ArrayList<PsGongcheng>?=null
    var xmlist:ArrayList<PsXiangmu>?= null
    var gxlist:ArrayList<PsGuanxian>?=null
    var jcjlist:ArrayList<PsJin>?= null
    var psklist:ArrayList<Pspfk>?= null
    var ysklist:ArrayList<Psyushuikou>?=null
    var lglist:ArrayList<PsLiguan>?= null

    private fun export(){



        mFragment.lifecycleScope.launch(Dispatchers.IO){
            ExcelUtils.instance.initExcel(FileUtil.getSDPath()+"/testexcel.xls")
            gclist= ArrayList()
            xmlist=ArrayList()
            gxlist= ArrayList()
            jcjlist= ArrayList()
            psklist=ArrayList()
            ysklist=ArrayList()
            lglist=ArrayList()
            if(dataList!=null){
                for(its in dataList!!){
                    if(its?.ifchecked==1){
                        val xm=AppDatabase.getInstance().userDao().getXmListbyId(its.ssxmId)
                        xm?.let {
                            xmlist?.add(it)
                        }
                       gclist?.add(its)
                        val gx= its.gcId?.let { it1 -> AppDatabase.getInstance().userDao().getgxBygcid(it1)

                        }
                        gx?.let {
                            gxlist?.add(gx)
                        }
                        val jcj = its.gcId?.let { it1 ->
                            AppDatabase.getInstance().userDao().getPsjin(
                                it1
                            )
                        }
                        jcj?.let {
                            jcjlist?.add(it)
                        }
                        val psk= its.gcId?.let { it1 ->
                            AppDatabase.getInstance().userDao().getPfkbygcid(
                                it1
                            )
                        }
                        psk?.let {
                            psklist?.add(it)
                        }
                        val ysk= its.gcId?.let { it1 ->
                            AppDatabase.getInstance().userDao().queyskbygcid(
                                it1
                            )
                        }
                        ysk?.let {
                            ysklist?.add(it)
                        }
                        val lg= its.gcId?.let { AppDatabase.getInstance().userDao().getLgbygcid(it) }
                        lg?.let {
                            lglist?.add(lg)
                        }



                    }
                }
                Log.d("jshns","shhs"+gxlist?.size)
                ExcelUtils.instance.initContent(xmlist,gclist,gxlist,jcjlist,psklist,ysklist,lglist)
                ExcelUtils.instance.writeObjListToExcel(FileUtil.getSDPath()+"/testexcel.xls")
//            mAdapter.notifyDataSetChanged()

            }

        }


    }
}